﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections;


namespace WaterLeverMonitor
{
    public partial class Form1 : Form
    {
        private string stime;//发送数据的时间
        private string strReceive;//存放下位机返回的数据
        private String s1;
        private bool XianChen = true;
        //private String t;
        //private int q;
        //private String u;
        private String u;
        Thread threadReceive;
        bool TestOff = true;
        int bytesRead;
        int len1;
        int Gbitlen = 0;//收到字节的长度
        int Sbitlen = 0;//发出的字节长度
        string TxtName;
        private ArrayList m_XAxis1;
        private ArrayList m_YAxis1;

        private ArrayList m_XAxis2;
        private ArrayList m_YAxis2;

        private ArrayList m_XAxis3;
        private ArrayList m_YAxis3;

        private ArrayList m_XAxis4;
        private ArrayList m_YAxis4;

        private ArrayList m_XAxis5;
        private ArrayList m_YAxis5;

        private ArrayList m_XAxis6;
        private ArrayList m_YAxis6;

        private ArrayList m_XAxis7;
        private ArrayList m_YAxis7;

        private ArrayList m_XAxis8;
        private ArrayList m_YAxis8;

        private ArrayList m_XAxis9;
        private ArrayList m_YAxis9;

        private ArrayList m_XAxis10;
        private ArrayList m_YAxis10;
        //private SerialPort serialPort2;
        public Form1()
        {
            InitializeComponent();

            System.Drawing.Drawing2D.GraphicsPath pathA1 = new System.Drawing.Drawing2D.GraphicsPath();
            
            
        }
        public ArrayList XAxis1
        {
            get { return m_XAxis1; }
            set { m_XAxis1 = value; }
        }

        public ArrayList YAxis1
        {
            get { return m_YAxis1; }
            set { m_YAxis1 = value; }
        }

        public ArrayList XAxis2
        {
            get { return m_XAxis2; }
            set { m_XAxis2 = value; }
        }

        public ArrayList YAxis2
        {
            get { return m_YAxis2; }
            set { m_YAxis2 = value; }
        }

        public ArrayList XAxis3
        {
            get { return m_XAxis3; }
            set { m_XAxis3 = value; }
        }

        public ArrayList YAxis3
        {
            get { return m_YAxis3; }
            set { m_YAxis3 = value; }
        }

        public ArrayList XAxis4
        {
            get { return m_XAxis4; }
            set { m_XAxis4 = value; }
        }

        public ArrayList YAxis4
        {
            get { return m_YAxis4; }
            set { m_YAxis4 = value; }
        }

        public ArrayList XAxis5
        {
            get { return m_XAxis5; }
            set { m_XAxis5 = value; }
        }

        public ArrayList YAxis5
        {
            get { return m_YAxis5; }
            set { m_YAxis5 = value; }
        }

        public ArrayList XAxis6
        {
            get { return m_XAxis6; }
            set { m_XAxis6 = value; }
        }

        public ArrayList YAxis6
        {
            get { return m_YAxis6; }
            set { m_YAxis6 = value; }
        }

        public ArrayList XAxis7
        {
            get { return m_XAxis7; }
            set { m_XAxis7 = value; }
        }

        public ArrayList YAxis7
        {
            get { return m_YAxis7; }
            set { m_YAxis7 = value; }
        }

        public ArrayList XAxis8
        {
            get { return m_XAxis8; }
            set { m_XAxis8 = value; }
        }

        public ArrayList YAxis8
        {
            get { return m_YAxis8; }
            set { m_YAxis8 = value; }
        }

        public ArrayList XAxis9
        {
            get { return m_XAxis9; }
            set { m_XAxis9 = value; }
        }

        public ArrayList YAxis9
        {
            get { return m_YAxis9; }
            set { m_YAxis9 = value; }
        }

        public ArrayList XAxis10
        {
            get { return m_XAxis10; }
            set { m_XAxis10 = value; }
        }

        public ArrayList YAxis10
        {
            get { return m_YAxis10; }
            set { m_YAxis10 = value; }
        }
        //将byte数组转换为16进制的数组
        private byte[] HexStringToByteArray(string s)
        {

            s = s.Replace(" ", "");//把字符串内的空格去掉

            byte[] buffer = new byte[s.Length / 2];//定义新的byte数组，存放转换后的字符
            
            
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);


           
            return buffer;
        }


        public delegate void InvokeDelegate();//定义委托
        private void SendBytesData(SerialPort serialPort)
        {
            string[] strSend = textBox1.Text.Trim().Split();
            byte[] bytesSend = new byte[strSend.Length];
            for (int i = 0; i < strSend.Length; i++)
            {
                bytesSend[i] = (byte)Convert.ToInt32(strSend[i], 16);

            }
            serialPort.Write(bytesSend, 0, bytesSend.Length);
        }

        
        public void Display()
        {
            //this.textBox3.SelectionStart = this.textBox1.Text.Length;
            //textBox3.SelectionLength = 0;
            //DateTime dt = DateTime.Now;
            //textBox3.Text =  s1;//textbox3显示端口接受到的数据
            int s3 = 0;
           
            try
            {
                s3 = s1.Length;
            }
            catch { }
            if (s3 != 0)
                richTextBox1.AppendText(stime + s1 );
            //listBox1.Items.Add(stime + s1);

            else
                richTextBox1.AppendText(stime );
                //listBox1.Items.Add(stime );
                
            //listBox1.SelectedIndex=listBox1.Items.Count-1;//滚动条在最后一行
            s1 = "";
            strReceive = "";
            stime = "";
            //this.richTextBox1.SelectionStart = s3 - len1-2 ;
            //this.richTextBox1.SelectionLength = len1;
            //this.richTextBox1.SelectionColor = Color.Black;
            Gbitlen += bytesRead;
            bytesRead = 0;
            //bitlen += ;
            toolStripStatusLabel2.Text = "收到的字节数：" + Gbitlen.ToString();
            

        }
        //同步阻塞读取
        private void SynReceiveData(object serialPortobj)
        {
            //System.Threading.Thread.Sleep(50);

            
            while (true)
            {
                SerialPort serialPort = (SerialPort)serialPortobj;                              
                try
                {                    
                    int lenght1 = serialPort.BytesToRead;
                    if (lenght1 != 0 && lenght1 != null)
                    {
                        //System.Threading.Thread.Sleep(25);
                        threadReceive.Join(10);
                        serialPort.ReadTimeout = 10000;
                        //阻塞到读取数据或超时(这里为2秒)
                        //  byte firstByte = Convert.ToByte(serialPort.ReadByte());
                        //int lenght1 = serialPort.BytesToRead;
                        bytesRead = serialPort.BytesToRead;
                        byte[] bytesData = new byte[bytesRead];
                        string dt = DateTime.Now.ToLongTimeString();
                        int dtm = DateTime.Now.Millisecond;
                        if (dtm < 100 && dtm >= 10)
                            strReceive += dt.ToString() + ":" + "0" + dtm.ToString() + ":" + "    ";
                        if (dtm < 10)
                            strReceive += dt.ToString() + ":" + "00" + dtm.ToString() + ":" + "    ";
                        //if (dtm ==0)
                        //    strReceive += dt.ToString() + ":" + "00" + dtm.ToString() + ":" + "    ";
                        if (dtm >= 100)
                            strReceive += dt.ToString() + ":" + dtm.ToString() + ":" + "    ";

                        string Sdate = "";
                        for (int i = 0; i < bytesRead; i++)
                        {
                            bytesData[i] = Convert.ToByte(serialPort.ReadByte());
                            //textBox3.Text += bytesData[i].ToString("X2") + " ";
                            strReceive += Convert.ToString(bytesData[i], 16).PadLeft(2, '0').PadRight(3, ' ').ToUpper();//将字符转化为字符串 strReceive
                            Sdate += Convert.ToString(bytesData[i], 16).PadLeft(2, '0').PadRight(3, ' ').ToUpper();
                        }
                        len1 = Sdate.Length;
                        //serialPort1.DiscardInBuffer();//释放接受缓冲区的数据
                        //if (stime != "")
                        //    strReceive = "接收的数据：" + strReceive + "\r\n";
                        //else
                            strReceive = "接收的数据：" + strReceive+"\n" ;

                        //Sdate = dt.ToString() + ":" + "    " + strReceive;

                        //Sdate = Sdate + "\r\n";
                        // s1 = strReceive;
                        s1 = strReceive;
                        //textBox3.BeginInvoke(new InvokeDelegate(Display));
                        richTextBox1.BeginInvoke(new InvokeDelegate(Display));
                        //listBox1.BeginInvoke(new InvokeDelegate(Display));


                    }
                    else
                    {
                        //System.Threading.Thread.Sleep(25);
                        threadReceive.Join(10);
                        serialPort.ReadTimeout = 10000;

                    }
                }

                catch //(Exception e)
                {
                    //MessageBox.Show(e.Message);
                    //处理超时错误
                }

                // serialPort.Close();
                
            }
            
        }
        
        public SerialPort SerialPort
        {

            get
            {
                return serialPort1;
            }
            set
            {
                serialPort1 = value;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Open();//打开端口
            }
            catch (Exception)
            {
                MessageBox.Show("Fail to open the COM port.\n   Please check the port name and the baud rate.", "Open COM Port Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        

        
        

        private void Form1_Load(object sender, EventArgs e)
        {
            //toolStripButton1.BackColor = Color.Red;
            FileStream fs = new FileStream(@"bote.txt", FileMode.Open, FileAccess.Read);
            StreamReader m_streamReader = new StreamReader(fs);
            //使用StreamReader类来读取文件 
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            // 从数据流中读取每一行，直到文件的最后一行，并在txtShow中显示出内容 
            string strLine;
            strLine = m_streamReader.ReadLine();
            toolStripTextBox1.Text = strLine;
            m_streamReader.Close();

            FileStream fe = new FileStream(@"set.txt", FileMode.Open, FileAccess.Read);
            StreamReader s_streamReader = new StreamReader(fe);
            //使用StreamReader类来读取文件 
            s_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            // 从数据流中读取每一行，直到文件的最后一行，并在txtShow中显示出内容 
            string stsLine;
            stsLine = s_streamReader.ReadLine();
            textBox1.Text = stsLine;
            
            s_streamReader.Close();
            toolStripButton7.Enabled = false;
            button1.Enabled = false;

            button2.Enabled = false;
            
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {

                    textBox2.Text = " ";
                }
                else
                {

                    string t;
                    int q;

                    string s = textBox1.Text;
                    int f = textBox1.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);

                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }
                        if (u == "ffff")
                            textBox2.Text = "";
                        else
                        {
                            u = u.Insert(2, " ");
                            textBox2.Text = u.ToUpper();
                        }
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }
                        if (u == "ffff")
                            textBox2.Text = "";
                        else
                        {
                            u = u.Insert(2, " ");
                            textBox2.Text = u.ToUpper();
                        }
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void ReceiveData(SerialPort serialPort)
        {
            //同步阻塞接收数据线程
            threadReceive = new Thread(new ParameterizedThreadStart(SynReceiveData));
            threadReceive.Start(serialPort);
            TestOff = false;
            
            //也可用异步接收数据线程
            //Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData));
            //threadReceiveSub.Start(serialPort);

        }
        

        

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            try
            {
                if (XianChen == false)
                    threadReceive.Resume();
                if (TestOff == false)
                    threadReceive.Abort();
                serialPort1.Close();//关闭端口
            }
            catch
            { }
            finally
            {
                if (TestOff == false)
                    threadReceive.Abort();
                serialPort1.Close();
            }
        }

        
        
       

       

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
            //this.textBox3.ScrollToCaret(); 
            //this.textBox3.Focus();//获取焦点
            //this.textBox3.Select(this.textBox3.TextLength, 0);//光标定位到文本最后

            this.richTextBox1.ScrollToCaret(); 
        }

      

        private void checkBox2_Click(object sender, EventArgs e)
        {
           
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                //if (textBox3.Text == "")
                //{
                //    //MessageBox.Show("请先输入文件名");
                //    checkBox2.Checked = false;
                //}
                if (checkBox2.Checked == true)
                {
                    TxtName = textBox3.Text + ".txt";
                    StreamWriter sw = File.CreateText(@TxtName);                     
                    sw.WriteLine(richTextBox1.Text);
                    sw.Flush();
                    sw.Close();

                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的文件名");
            }
        }

        private void toolStripComboBox1_DropDown(object sender, EventArgs e)
        {
            try
            {
                toolStripComboBox1.Items.Clear();//讲combobox中的端口名丢弃
                string[] portnames = SerialPort.GetPortNames();//获取当前串行端口的名称数组
                for (int i = 0; i < portnames.Length; i++)
                    toolStripComboBox1.Items.Add(portnames[i]);//讲端口名添加到combobox中的集合中
            }
            catch { }
        }

        private void toolStripComboBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = toolStripComboBox1.SelectedItem.ToString();//端口变化时，串口名改变
            }
            catch { }
        }
        bool Open = true;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Open == true)
                {

                    serialPort1.Open();//打开端口

                    ReceiveData(serialPort1);
                    toolStripButton1.Text = "关闭";
                    Open = false;
                    toolStripButton1.BackColor = Color.Lime;
                    toolStripButton7.Enabled = true;
                    button1.Enabled = true;
                }
                else
                {
                    if (XianChen == false)
                        threadReceive.Resume();
                    threadReceive.Abort();
                    serialPort1.Close();//关闭端口

                    
                    toolStripButton1.Text = "打开";
                    Open = true;
                    toolStripButton1.BackColor = Color.Red;
                    toolStripButton7.Enabled = false;
                    button1.Enabled = false;
                }
            }
            catch { }
        }

        

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            new Form2(this);//打开端口设置菜单
        }


        public bool time = true;
        XunHuan Xh ;

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            if (toolStripButton7.Text == "停止发送")
            {
                toolStripButton1.Enabled = true;
                toolStripButton7.Text = "定时发送";
                try
                {
                    XAxis1.Clear();
                    YAxis1.Clear();

                    timer1.Stop();
                }
                catch { }
                try
                {
                    XAxis2.Clear();
                    YAxis2.Clear();

                    timer2.Stop();
                }
                catch { }
                try
                {
                    XAxis3.Clear();
                    YAxis3.Clear();

                    timer3.Stop();
                }
                catch { }
                try
                {
                    XAxis4.Clear();
                    YAxis4.Clear();

                    timer4.Stop();
                }
                catch { }
                try
                {
                    XAxis5.Clear();
                    YAxis5.Clear();

                    timer5.Stop();
                }
                catch { }
                try
                {
                    XAxis6.Clear();
                    YAxis6.Clear();

                    timer6.Stop();
                }
                catch { }
                try
                {
                    XAxis7.Clear();
                    YAxis7.Clear();

                    timer7.Stop();
                }
                catch { }
                try
                {
                    XAxis8.Clear();
                    YAxis8.Clear();

                    timer8.Stop();
                }
                catch { } 
                try
                {
                    XAxis9.Clear();
                    YAxis9.Clear();

                    timer9.Stop();
                }
                catch { } 
                try
                {
                    XAxis10.Clear();
                    YAxis10.Clear();

                    timer10.Stop();
                }
                catch { }
                //Xh.timer2.Stop();
                //Xh.timer3.Stop();
                //Xh.timer4.Stop();
                //Xh.timer5.Stop();
                //Xh.timer6.Stop();
                //Xh.timer7.Stop();
                //Xh.timer8.Stop();
                //Xh.timer9.Stop();
                //Xh.timer10.Stop();

                //Xh.Focus();
                //Xh.Close();
                //Xh.Focus();
                
                
            }
            else
            {
                if (Xh == null || Xh.IsDisposed)
                {
                    Xh = new XunHuan(this);
                }
                

                Xh.ShowDialog();
                
            }
            
        }

        

       

       

       

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

        private void button1_Click_1(object sender, EventArgs e)
        {
            
            StreamWriter sw = new StreamWriter(@"bote.txt");
            sw.WriteLine(toolStripTextBox1.Text + "\n");
            sw.Flush();
            sw.Close();
            StreamWriter ss = new StreamWriter(@"set.txt");
            ss.WriteLine(textBox1.Text + "\n");
            ss.Flush();
            ss.Close();
            string ComString = textBox1.Text;//讲输入的字符赋值给s           
            try
            {
                if (checkBox1.Checked == true)
                {
                    ComString = textBox1.Text + " " + textBox2.Text;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";
                    //if (dtm ==0)
                    //    strReceive += dt.ToString() + ":" + "00" + dtm.ToString() + ":" + "    ";
                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += textBox1.Text + " " + textBox2.Text;
                }
                else
                {
                    string dt = DateTime.Now.ToLongTimeString();
                    int dtm = DateTime.Now.Millisecond;
                    if (dtm <= 100 && dtm > 10)
                        stime += dt.ToString() + ":" + "0" + dtm.ToString() + ":" + "    ";
                    if (dtm <= 10)
                        stime += dt.ToString() + ":" + "00" + dtm.ToString() + ":" + "    ";
                   
                    if (dtm > 100)
                        stime += dt.ToString() + ":" + dtm.ToString() + ":" + "    ";
                    stime += textBox1.Text;
                }
                stime = "发送的数据：" + stime+"\n";                
                Display();            
                string s2 = ComString;
                s2 = s2.Replace(" ", "");
                Sbitlen += s2.Length / 2;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(ComString);//调用16进制的转换方法               
                serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        bool StopTime = true;
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (StopTime == true)
                {
                    threadReceive.Suspend();//线程挂起
                    //richTextBox1.Text = strReceive;
                    XianChen = false;
                    button2.Text = "继续显示";
                    StopTime = false;
                    toolStripButton1.Enabled = false;
                }
                else
                {
                    XianChen = true;
                    threadReceive.Resume();//恢复线程
                    button2.Text = "暂停显示";
                    StopTime = true;
                    toolStripButton1.Enabled = true;
                }
            }
            catch { }
        }

        

        private void toolStripButton8_Click(object sender, EventArgs e)
        {

        }

       
        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";//清空接受文本框
            strReceive = "";//清空全局变量
            s1 = "";
            Gbitlen = 0;
            toolStripStatusLabel2.Text = "收到的字节数："+"0";
        
        }

        private void toolStripStatusLabel4_Click(object sender, EventArgs e)
        {
            Sbitlen = 0;
            len1 = 0;
            toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
            textBox1.Text = "";
        }
        private string strFilePath;
        private void SaveFile(int intSaveType)
        {
            // 设定可以保存文件的类型
            this.saveFileDialog1.Filter = "Text Files|*.txt;*.text|All Files|*.*";
            // 使用saveFileDialog在指定位置保存文件
            try
            {
                
                if (this.strFilePath != null && this.strFilePath.Length <= 0 && intSaveType != 2)
                {
                    this.saveFileDialog1.FileName = this.strFilePath;
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        this.richTextBox1.SaveFile(strFilePath);
                    }
                }
                else
                {
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        // 没有指定的时候
                        richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SaveFile(1);
        }
        int tm = 0;
        public void Time1()
        {
            
            timer1.Enabled = true;
            
        }
        public void Time2()
        {
            tm =  int.Parse(Xh.textBox12.Text)+int.Parse(Xh.textBox11.Text);
            timer2.Interval = tm;
            timer1.Enabled = false;
            
            

        }
        public void Time3()
        {
            timer3.Interval = Convert.ToInt32(Xh.textBox13.Text);
            timer3.Start();

        }
        public void Time4()
        {
            timer4.Interval = Convert.ToInt32(Xh.textBox14.Text);
            timer4.Start();

        }
        public void Time5()
        {
            timer5.Interval = Convert.ToInt32(Xh.textBox15.Text);
            timer5.Start();

        }
        public void Time6()
        {
            timer6.Interval = Convert.ToInt32(Xh.textBox16.Text);
            timer6.Start();

        }
        public void Time7()
        {
            timer7.Interval = Convert.ToInt32(Xh.textBox17.Text);
            timer7.Start();

        }
        public void Time8()
        {
            timer8.Interval = Convert.ToInt32(Xh.textBox18.Text);
            timer8.Start();

        }
        public void Time9()
        {
            timer9.Interval = Convert.ToInt32(Xh.textBox19.Text);
            timer9.Start();

        }
        public void Time10()
        {
            timer10.Interval = Convert.ToInt32(Xh.textBox20.Text);
            timer10.Start();

        }
        private void timer1_Tick(object sender, EventArgs e)
        {

                tm = int.Parse(Xh.textBox11.Text);
                timer1.Interval = tm;
                int lengthx = m_XAxis1.Count;

                string[] strx = new string[lengthx];

                for (int i = 0; i < lengthx; i++)
                {
                    strx[i] = m_XAxis1[i].ToString();
                }

                string ComString = "";
                for (int i = 0; i < lengthx; i++)
                {
                    ComString += strx[i].ToString();
                }
                string ComString2 = "";
                try
                {

                    int lengthy = m_YAxis1.Count;
                    string[] stry = new string[lengthy];
                    for (int i = 0; i < lengthy; i++)
                    {
                        stry[i] = m_YAxis1[i].ToString();
                    }
                    for (int i = 0; i < lengthy; i++)
                    {
                        ComString2 += stry[i].ToString();
                    }
                }
                catch
                { }

                

                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;

                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                //}
                //catch
                //{
                if (Xh.cb2 == true)
                {
                    timer1.Enabled = false;
                    timer2.Enabled = true;
                }
                
                
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                tm = int.Parse(Xh.textBox12.Text);
                timer2.Interval = tm;
                //timer1.Enabled = false;
                int lengthx = m_XAxis2.Count;

                string[] strx = new string[lengthx];

                for (int i = 0; i < lengthx; i++)
                {
                    strx[i] = m_XAxis2[i].ToString();
                }

                string ComString = "";
                for (int i = 0; i < lengthx; i++)
                {
                    ComString += strx[i].ToString();
                }
                string ComString2 = "";
                try
                {

                    int lengthy = m_YAxis2.Count;
                    string[] stry = new string[lengthy];
                    for (int i = 0; i < lengthy; i++)
                    {
                        stry[i] = m_YAxis2[i].ToString();
                    }
                    for (int i = 0; i < lengthy; i++)
                    {
                        ComString2 += stry[i].ToString();
                    }
                }
                catch
                { }

                try
                {
                    //if (checkBox1.Checked == true)
                    //{
                    //    ComString = textBox1.Text + " " + textBox21.Text;
                    //}
                    string s2;
                    if (ComString2 != "")
                    {
                        s2 = ComString + ComString2;

                        string dt1 = DateTime.Now.ToLongTimeString();
                        int dtm1 = DateTime.Now.Millisecond;
                        if (dtm1 <= 100 && dtm1 > 10)
                            stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                        if (dtm1 <= 10)
                            stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                        if (dtm1 > 100)
                            stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                        stime += s2;
                    }
                    else
                    {
                        s2 = ComString;
                        string dt1 = DateTime.Now.ToLongTimeString();
                        int dtm1 = DateTime.Now.Millisecond;
                        if (dtm1 <= 100 && dtm1 > 10)
                            stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                        if (dtm1 <= 10)
                            stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                        if (dtm1 > 100)
                            stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                        stime += s2;
                    }
                    s2 = s2.Replace(" ", "");
                    int len = s2.Length / 2;
                    stime = "发送的数据：" + stime + "\r\n";
                    Display();
                    Sbitlen += len;
                    toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                    byte[] bytesend = HexStringToByteArray(s2);
                    textBox1.Text = s2;
                    //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                    //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                    serialPort1.Write(bytesend, 0, bytesend.Length);
                    s2 = "";
                    ComString = "";
                    ComString2 = "";
                    //timer1.Enabled = true;
                    
                    //timer1.Enabled = true;
                    if (Xh.cb3 == true)
                    {
                        timer2.Enabled = false;
                        timer3.Enabled = true;

                    }
                    else
                    {
                        timer2.Enabled = false;
                        timer1.Enabled = true;

                    }

                }
                catch { }
            }
            catch { }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox13.Text);
            timer3.Interval = tm;
            int lengthx = m_XAxis3.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis3[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis3.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis3[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                //s2 = s2.Replace(" ", "");
                //int len = s2.Length / 2;
                //stime = "发送的数据：" + stime + "\r\n";
                //Sbitlen += len;
                //toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                //byte[] bytesend = HexStringToByteArray(s2);
                //textBox1.Text = s2;
                ////byte[] bytesend = Encoding.Default.GetBytes(ComString);
                ////serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                //serialPort1.Write(bytesend, 0, bytesend.Length);
                //s2 = "";
                //ComString = "";
                //ComString2 = "";
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb4 == true)
                {
                    timer3.Enabled = false;
                    timer4.Enabled = true;
                }
                else
                {
                    timer3.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox14.Text);
            timer4.Interval = tm;
            int lengthx = m_XAxis4.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis4[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis4.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis4[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb5 == true)
                {
                    timer4.Enabled = false;
                    timer5.Enabled = true;
                }
                else
                {
                    timer4.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer5_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox15.Text);
            timer5.Interval = tm;
            int lengthx = m_XAxis5.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis5[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis5.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis5[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb6 == true)
                {
                    timer5.Enabled = false;
                    timer6.Enabled = true;
                }
                else
                {
                    timer5.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer6_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox16.Text);
            timer6.Interval = tm;
            int lengthx = m_XAxis6.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis6[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis6.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis6[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb7 == true)
                {
                    timer6.Enabled = false;
                    timer7.Enabled = true;
                }
                else
                {
                    timer6.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer7_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox17.Text);
            timer7.Interval = tm;
            int lengthx = m_XAxis7.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis7[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis7.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis7[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb8 == true)
                {
                    timer7.Enabled = false;
                    timer8.Enabled = true;
                }
                else
                {
                    timer7.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer8_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox18.Text);
            timer8.Interval = tm;
            int lengthx = m_XAxis8.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis8[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis8.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis8[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb9 == true)
                {
                    timer8.Enabled = false;
                    timer9.Enabled = true;
                }
                else
                {
                    timer8.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer9_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox19.Text);
            timer9.Interval = tm;
            int lengthx = m_XAxis9.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis9[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis9.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis9[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                if (Xh.cb10 == true)
                {
                    timer9.Enabled = false;
                    timer10.Enabled = true;
                }
                else
                {
                    timer9.Enabled = false;
                    timer1.Enabled = true;
                }
            }
            catch { }
        }

        private void timer10_Tick(object sender, EventArgs e)
        {
            tm = int.Parse(Xh.textBox20.Text);
            timer10.Interval = tm;
            int lengthx = m_XAxis10.Count;

            string[] strx = new string[lengthx];

            for (int i = 0; i < lengthx; i++)
            {
                strx[i] = m_XAxis10[i].ToString();
            }

            string ComString = "";
            for (int i = 0; i < lengthx; i++)
            {
                ComString += strx[i].ToString();
            }
            string ComString2 = "";
            try
            {

                int lengthy = m_YAxis10.Count;
                string[] stry = new string[lengthy];
                for (int i = 0; i < lengthy; i++)
                {
                    stry[i] = m_YAxis10[i].ToString();
                }
                for (int i = 0; i < lengthy; i++)
                {
                    ComString2 += stry[i].ToString();
                }
            }
            catch
            { }

            try
            {
                //if (checkBox1.Checked == true)
                //{
                //    ComString = textBox1.Text + " " + textBox21.Text;
                //}
                string s2;
                if (ComString2 != "")
                {
                    s2 = ComString + ComString2;

                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                else
                {
                    s2 = ComString;
                    string dt1 = DateTime.Now.ToLongTimeString();
                    int dtm1 = DateTime.Now.Millisecond;
                    if (dtm1 <= 100 && dtm1 > 10)
                        stime += dt1.ToString() + ":" + "0" + dtm1.ToString() + ":" + "    ";
                    if (dtm1 <= 10)
                        stime += dt1.ToString() + ":" + "00" + dtm1.ToString() + ":" + "    ";

                    if (dtm1 > 100)
                        stime += dt1.ToString() + ":" + dtm1.ToString() + ":" + "    ";
                    stime += s2;
                }
                s2 = s2.Replace(" ", "");
                int len = s2.Length / 2;
                stime = "发送的数据：" + stime + "\r\n";
                Display();
                Sbitlen += len;
                toolStripStatusLabel1.Text = "发送的字节数：" + Sbitlen.ToString();
                byte[] bytesend = HexStringToByteArray(s2);
                textBox1.Text = s2;
                //byte[] bytesend = Encoding.Default.GetBytes(ComString);
                //serialPort1.Write(bytesend, 0, bytesend.Length);//往端口写入数据
                serialPort1.Write(bytesend, 0, bytesend.Length);
                s2 = "";
                ComString = "";
                ComString2 = "";
                
                timer10.Enabled = false;
                timer1.Enabled = true;
                
            }
            catch { }
        }

        private void toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                serialPort1.BaudRate = Convert.ToInt32(toolStripTextBox1.Text);
                
                
            }
            catch { }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

       











    }
}
